﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCSLibrary;
using Utils.DC.Web.UI;
using Utils.DatabaseUtils;

public partial class Admin_Form_Document_Add : System.Web.UI.Page
{
    CategoryPageUI objCategoryPage = new CategoryPageUI();
    DocumentUI objDocument = new DocumentUI();
    DataSet dsCategoryPage;

    protected void Page_Load(object sender, EventArgs e)
    {
        dsCategoryPage = objCategoryPage.GetAll();
        if (!IsPostBack)
        {
            BindCategoryPage();
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }


    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        string alias = Utils.Format.ConvertToNoSign(txtDocumentName.Text.Trim());
        if (txtDocumentID.Value.Trim() == "")
        {
            try
            {
                int result = objDocument.Add(Convert.ToInt32(cboCategoryPage.SelectedValue), txtDocumentName.Text.Trim(), alias, txtDocumentImage.Text.Trim(), txtDocumentPath.Text.Trim(),
                    txtMetaKeyword.Text.Trim(), txtMetaDescription.Text.Trim(), txtDocumentType.Text.Trim(), Convert.ToDecimal(txtDocumentSize.Text.Trim()), chkIsHot.Checked, chkIsTypical.Checked, chkIsNew.Checked, 0, 0, Convert.ToInt32(txtViewOrder.Text.Trim()));

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                lblMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objDocument.Update(Convert.ToInt32(txtDocumentID.Value), Convert.ToInt32(cboCategoryPage.SelectedValue), txtDocumentName.Text.Trim(), alias, txtDocumentImage.Text.Trim(), txtDocumentPath.Text.Trim(),
                    txtMetaKeyword.Text.Trim(), txtMetaDescription.Text.Trim(), txtDocumentType.Text.Trim(), Convert.ToDecimal(txtDocumentSize.Text.Trim()), chkIsHot.Checked, chkIsTypical.Checked, chkIsNew.Checked, 0, 0, Convert.ToInt32(txtViewOrder.Text.Trim()));

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-chung/quan-tri-trang-nhom.aspx");
    }

    #region Cac ham tuong tac du lieu

    private void BindCategoryPage()
    {
        try
        {
            cboCategoryPage.DataSource = TableLib.CreateTable(dsCategoryPage.Tables[0], "FunctionPageAlias = 'tai-lieu'");
            cboCategoryPage.DataBind();
        }
        catch (Exception e)
        {

        }
    }

    private void LoadEditData(int DocumentID)
    {
        DataSet ds = objDocument.GetByID(DocumentID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtDocumentID.Value = row["DocumentID"].ToString();

            txtMetaDescription.Text = row["MetaDescription"].ToString();
            txtMetaKeyword.Text = row["MetaKeyword"].ToString();
            txtDocumentImage.Text = row["DocumentImage"].ToString();
            txtDocumentName.Text = row["DocumentName"].ToString();
            txtDocumentPath.Text = row["DocumentPath"].ToString();
            txtDocumentSize.Text = row["DocumentSize"].ToString();
            txtDocumentType.Text = row["DocumentType"].ToString();
            txtViewOrder.Text = row["ViewOrder"].ToString();
            chkIsTypical.Checked = Convert.ToBoolean(row["IsTypical"]);
            chkIsHot.Checked = Convert.ToBoolean(row["IsHot"]);
            chkIsNew.Checked = Convert.ToBoolean(row["IsNew"]);
            General.SetSelectedItemInComboBox(row["CategoryPageID"].ToString(), cboCategoryPage);
        }
    }

    private void ReInsertData()
    {
        //txtDocumentID.Value = "";

        txtMetaDescription.Text = "";
        txtMetaKeyword.Text = "";
        txtDocumentImage.Text = "";
        txtDocumentName.Text = "";
        txtDocumentPath.Text = "";
        txtDocumentSize.Text = "";
        txtDocumentType.Text = "";
        txtViewOrder.Text = "";
        chkIsTypical.Checked = false;
        chkIsHot.Checked = false;
        chkIsNew.Checked = false;

        General.SetSelectedItemInComboBox(null, cboCategoryPage);
    }

    #endregion
}
